/*
* Bitmap Font for managing position and sizes of each glyph within the font spritesheet.
*/
#pragma once

#include <string>
#include <memory>
#include <vector>
#include "ext/stb_truetype.h"
#include <GL/glew.h>
#include <glm/glm.hpp>

namespace teflon
{
    struct GlyphInfo
    {
        glm::vec3 positions[4];
        glm::vec2 uvs[4];
        float offsetX = 0;
        float offsetY = 0;
    };

    struct FontData
    {
        const uint32_t size = 40;
        const uint32_t atlasWidth = 1024;
        const uint32_t atlasHeight = 1024;
        const uint32_t oversampleX = 2;
        const uint32_t oversampleY = 2;
        const uint32_t firstChar = ' ';
        const uint32_t charCount = '~' - ' ';
        std::unique_ptr<stbtt_packedchar[]> charInfo;
        GLuint texture = 0;
    };

    class BFont
    {
    private:
        FontData font_;

    public:
        BFont();
        ~BFont();

        void Init(const std::string& fontfile);

        void MapVertex(const std::string& text, 
            std::vector<glm::vec3>& positions,
            std::vector<glm::vec2>& uvs,
            std::vector<uint16_t>& indices,
            float scale);
        GlyphInfo makeGlyphInfo(uint32_t character, float offsetX, float offsetY) const;

        void Bind() const;
        void Unbind() const;

        GLuint GetTextureId() const { return font_.texture; }
    private:
        std::vector<uint8_t> readFile(const std::string& filepath);
        void cleanUp();
    };
}
